<template>
    <view>
        <view class="log" v-for="(item, index) in logs" :key="index">{{item}}</view>
    </view>
</template>

<script>
    import getLocation from '@/utils/getLocation.js'
    import {
        mapState,
        mapMutations
    } from 'vuex';
    import mathUtil from '@/utils/mathUtil.js'
    export default {
        mixins: [getLocation],
        data() {
            return {
                logs: ['日志如下：']
            }
        },
        computed: {
            ...mapState(['lnglat', 'userPosition'])
        },
        methods: {
            ...mapMutations(['setPosition']),
            startLogPosition: function() {
                var vm = this;
                uni.setKeepScreenOn({
                    keepScreenOn: true
                });
                vm.doWxLogin().then(res => {
                    // debugger
                    vm.openId = res.openId;
                    if (wx.canIUse('startLocationUpdate')) {
                        uni.$u.toast('轨迹移动数据将被上报');
                        vm.logPostionByListen();
                    } else {
                        vm.logPosition();
                        setInterval(() => {
                            vm.logPosition();
                        }, 15000);
                    }
                });
            },
            logPosition() {
                var vm = this;
                vm.getUserLocation().then(res => {
                    vm.setPosition(res);
                    vm.getAndLoad('上报点位',
                        `/mobile/api/uploadLoc.jsp?wxid=${vm.openId}&lng=${res.longitude}&lat=${res.latitude}`,
                        function() {
                            vm.logs.push(`点位上报成功：lng=${res.longitude}&lat=${res.latitude}`);
                        });
                });
            },
            logPostionByListen: function() {
                var vm = this;
                vm.listenUserLocation(res => {
                    vm.setPosition(res);
                    var time = new Date().getTime();
                    // 如果移动距离超过30米 或者 超过10秒未上报，则上报
                    if (!global.lastLocationReportTime ||
                        (time - global.lastLocationReportTime > 10000) ||
                        mathUtil.getDistance(vm.lnglat[1], vm.lnglat[0], res.latitude, res.longitude) > 30
                    ) {
                        vm.getAndLoad('上报点位',
                            `/mobile/api/uploadLoc.jsp?wxid=${vm.openId}&lng=${res.longitude}&lat=${res.latitude}`,
                            function() {
                             	  vm.logs.push(`监听点位成功：lng=${res.longitude}&lat=${res.latitude}`);
                                global.lastLocationReportTime = time;
                            });
                    } else {
                        vm.logs.push(`监听未上报：lng=${res.longitude}&lat=${res.latitude}`);
                    }

                })
            }
        },
        onLoad(options) {
            var vm = this;
            // debugger
            wx.showShareMenu({
                withShareTicket: true,
                menus: ["shareAppMessage", "shareTimeline"]
            });
            vm.startLogPosition();
        },
    }
</script>

<style lang="scss" scoped>
    .log {
        font-size: 12px !important;
    }
</style>
